package me.minecraft.woodenman.command;

import me.minecraft.woodenman.config.Language;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;

/**
 * @author langle__
 * @version 1.0
 */
public class MainCommand implements TabExecutor {

    private final Map<String, SubCommand> subCommands = new HashMap<>();

    public MainCommand() {
        this.addSubCommand(new CreateCommand());
        this.addSubCommand(new StartCommand());
        this.addSubCommand(new StopCommand());
        this.addSubCommand(new SetCommand());
    }

    @Override
    public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
        if (args.length == 0) {
            // TODO 发送命令提示
            sender.sendMessage("§aWoodenMan v1.0");
            return false;
        }
        String cmd = args[0].toLowerCase();
        if (subCommands.containsKey(cmd)) {
            subCommands.get(cmd).onCommand(sender, args);
        } else {
            sender.sendMessage(Language.UNKNOWN_SUBCOMMAND);
        }
        return true;
    }

    @Nullable
    @Override
    public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
        if (args.length == 1) {
            return Arrays.asList("create", "start", "stop", "set");
        }
        if (args.length == 2) {
            if ("set".equalsIgnoreCase(args[0])) {
                return Arrays.asList("spawn", "woodenman");
            }
        }
        return Collections.emptyList();
    }

    private void addSubCommand(SubCommand cmd) {
        this.subCommands.put(cmd.getName().toLowerCase(), cmd);
    }

}
